package com.xiaoyu.lanling.feature.gift.fragment

import android.os.Bundle
import android.view.View
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.gift.GiftListEvent
import com.xiaoyu.lanling.feature.gift.model.Gift
import com.xiaoyu.lanling.features.chat.view.AbstractGiftFragment
import com.xiaoyu.lanling.view.OnShowFragment
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.ArrayList

class NormalGiftFragment : AbstractGiftFragment<Gift>() {

    companion object {
        fun create(): OnShowFragment {
            val params = Bundle()

            val fragment: OnShowFragment = NormalGiftFragment()
            fragment.arguments = params
            return fragment
        }
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initEvent()
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GiftListEvent) {
                val fragments = generateFragments(event.normalGifts);
                initFragmentAdapter(fragments)
            }
        })
    }

    override fun getLayoutID(): Int {
        return R.layout.gift_normal_fragment
    }

    override fun createItemFragment(giftList: List<Gift>): OnShowFragment {
        return NormalGiftListFragment.create(ArrayList(giftList))
    }
}